package com.example.sys

import androidx.lifecycle.viewModelScope
import com.example.lib_common.mvi.BaseViewModel
import com.example.lib_common.mvi.ResponseEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class MainViewModel : BaseViewModel<MainRepository,MainState>(){
    override fun createRepo(): MainRepository {
        return MainRepository()
    }

    override fun createState(): MainState {
        return MainState(ResponseEntity(200,"",HomeFeedEntity()))
    }
    fun requestTest(){
        mutableContainer.updateRequestState { copy(
            request=true
        ) }
        viewModelScope.launch(Dispatchers.IO){
            repo.requestHot().flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState {
                        copy(request=false,complete=true)
                    }
                    mutableContainer.updateUiState {
                        copy(entity=it)
                    }
                }
        }
    }
}